import 'package:app_admin/models/order_set_info.dart';
import 'package:app_admin/res/strings.dart';
import 'package:app_admin/res/styles.dart';
import 'package:app_admin/utils/utils.dart';
import 'package:app_admin/widgets/dialog.dart';
import 'package:app_admin/widgets/views.dart';
import 'package:common_utils/common_utils.dart';

import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker_plus/flutter_datetime_picker_plus.dart'
    as pk;
import 'package:flutter_datetime_picker_plus/flutter_datetime_picker_plus.dart';
import 'package:flutter_pickers/pickers.dart';
import 'package:get/get.dart';

class OrderSetAddLogic extends GetxController {
  final priceCtrl = TextEditingController();
  final totalpriceCtrl = TextEditingController();
  final numCtrl = TextEditingController();
  final time = "请选择日期".obs;
  final orderPlayer = "请选择订货商".obs;
  final orderGoods = "请选择物料名称".obs;
  final orderLevel = "请选择品味规格".obs;
  final orderPrice = 0.obs;
  final ordertotal = 0.obs;
  final orderNum = 0.obs;

  final titleName = "新增订货单设置".obs;

  String? id;

  @override
  void onInit() {
    var arguments = Get.arguments;
    if (arguments != null) {
      titleName.value = "订货单设置详情";
      OrderSetInfo dt = arguments as OrderSetInfo;
      id = dt.id;
      time.value = dt.orderTime ?? "请选择日期";
      orderPlayer.value = dt.orderPlayerName ?? "请选择订货商";
      orderGoods.value = dt.goods ?? "请选择物料名称";
      orderLevel.value = dt.level ?? "请选择品味规格";
      priceCtrl.text = dt.price.toString();
      totalpriceCtrl.text = dt.total.toString();
      numCtrl.text = dt.num.toString();
    }
  }

  ///订货日期
  void openDatePicker() {
    var appLocale = Get.locale;
    var isZh = appLocale!.languageCode.toLowerCase().contains("zh");
    pk.DatePicker.showDatePicker(Get.context!,
        locale: isZh ? LocaleType.zh : LocaleType.en,
        maxTime: DateTime.now(),
        theme: pk.DatePickerTheme(
          cancelStyle: Styles.ts_0C1C33_17sp,
          doneStyle: Styles.ts_0089FF_17sp,
          itemStyle: Styles.ts_0C1C33_17sp,
        ),
        onConfirm: updateTime);
  }

  void updateTime(DateTime dateTime) {
    var t = dateTime.millisecondsSinceEpoch;
    time.value = DateUtil.formatDateMs(
      t ?? 0,
      format: IMUtils.getTimeFormat1(),
    );
    // LoadingView.singleton.wrap(
    //   asyncFunction: () => Apis.updateUserInfo(
    //     userID: OpenIM.iMManager.userID,
    //     birth: birthday * 1000,
    //   ).then((value) => imLogic.userInfo.update((val) {
    //         val?.birth = birthday * 1000;
    //       })),
    // );
  }

  ///订货商
  void openOrderPlayerSelect(BuildContext context) {
    Pickers.showSinglePicker(context,
        data: ['PHP', 'JAVA', 'C++', 'Dart', 'Python', 'Go'],
        selectData: orderPlayer.value,
        onConfirm: openOrderPlayerOk);
  }

  void openOrderPlayerOk(p, index) {
    orderPlayer.value = p;
  }

  ///订货商
  void openOrderGoodsSelect(BuildContext context) {
    Pickers.showSinglePicker(context,
        data: ['PHP', 'JAVA', 'C++', 'Dart', 'Python', 'Go'],
        selectData: orderGoods.value,
        onConfirm: openOrderGoodsOk);
  }

  void openOrderGoodsOk(p, index) {
    orderGoods.value = p;
  }

  ///品味规格
  void openOrderLevelSelect(BuildContext context) {
    Pickers.showSinglePicker(context,
        data: ['PHP', 'JAVA', 'C++', 'Dart', 'Python', 'Go'],
        selectData: orderLevel.value,
        onConfirm: openOrderLevelOk);
  }

  void openOrderLevelOk(p, index) {
    orderLevel.value = p;
  }

  void btnSave(BuildContext context) async {
    OrderSetInfo dt = OrderSetInfo();
    dt.id = id;
    dt.orderTime = time.value == "请选择日期" ? null : time.value;
    dt.orderPlayerName =
        orderPlayer.value == "请选择订货商" ? null : orderPlayer.value;
    dt.goods = orderGoods.value == "请选择物料名称" ? null : orderGoods.value;
    dt.level = orderLevel.value == "请选择品味规格" ? null : orderLevel.value;
    dt.price = priceCtrl.text == "" ? null : double.parse(priceCtrl.text);
    dt.total =
        totalpriceCtrl.text == "" ? null : double.parse(totalpriceCtrl.text);
    dt.num = numCtrl.text == "" ? null : int.parse(numCtrl.text);

    if (dt.orderTime == null) {
      IMViews.showToast("日期为空");
      return;
    }
    if (dt.orderPlayerName == null) {
      IMViews.showToast("订货商为空");
      return;
    }
    if (dt.goods == null) {
      IMViews.showToast("物料为空");
      return;
    }
    if (dt.level == null) {
      IMViews.showToast("品味规格为空");
      return;
    }
    if (dt.price != null) {
      IMViews.showToast("单价为空");
      return;
    }
    if (dt.total != null) {
      IMViews.showToast("总价为空");
      return;
    }
    if (dt.num != null) {
      IMViews.showToast("订货单为空");
      return;
    }

    var confirm = await Get.dialog(const CustomDialog(
      title: "提示",
      content: "保存成功!",
    ));
    if (confirm == true) {
      Get.back();
    }
  }
}
